﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using System.Configuration;
using System.Diagnostics;

namespace CloudStorageLight.Core.Tests
{
    [TestClass]
    public class OneDriveBlobAdapterTest
    {
        public string ClientId = ConfigurationManager.AppSettings["OneDriveClientId"];
        public string ClientSecret = ConfigurationManager.AppSettings["OneDriveClientSecret"];
        public string AccessToken = ConfigurationManager.AppSettings["OneDriveAccessToken"];
        public string RefreshToken = ConfigurationManager.AppSettings["OneDriveRefreshToken"];



        [TestMethod]
        public void ConvertUrlPathToFtpPathTest()
        {
            using (var adp = new OneDriveBlobAdapter("/OneDrive/", ClientId, ClientSecret, AccessToken, RefreshToken, "/Public/", null))
            {
                var value = adp.ConvertUrlPathToOneDrivePath("/OneDrive/Sub1/A.txt");

                Assert.AreEqual("/Public/Sub1/A.txt", value);

            }
        }

        [TestMethod]
        public void UploadAsyncAndDeleteAsyncCopyAsyncTest()
        {
            using (var adp = new OneDriveBlobAdapter("/OneDrive/", ClientId, ClientSecret, AccessToken, RefreshToken, "/Public/", null))
            {
                var temp = Path.GetTempFileName();
                try
                {
                    var itemA = new BlobFile(null, "/OneDrive/A.txt");
                    var itemB = new BlobFile(null, "/OneDrive/Sub1/Sub2/B.txt");
                    var itemBDire = new BlobDirectory(null, "/OneDrive/Sub1/");
                    var itemB2Dire = new BlobDirectory(null, "/OneDrive/Sub1/Sub2/");
                    adp.DeleteAsync(itemA).Wait();
                    adp.DeleteAsync(itemB).Wait();
                    adp.DeleteAsync(itemB2Dire).Wait();
                    adp.DeleteAsync(itemBDire).Wait();

                    var value = adp.ExistsAsync("/OneDrive/A.txt").Result;
                    Assert.AreEqual(false, value);

                    adp.UploadAsync(itemA, temp).Wait();
                    value = adp.ExistsAsync("/OneDrive/A.txt").Result;
                    Assert.AreEqual(true, value);

                    adp.CopyAsync("/OneDrive/Sub1/Sub2/B.txt", itemA).Wait();
                    value = adp.ExistsAsync("/OneDrive/Sub1/Sub2/B.txt").Result;
                    Assert.AreEqual(true, value);

                }
                finally
                {
                    File.Delete(temp);
                }

            }
        }

        [TestMethod]
        public void GetFilesAsyncTest()
        {
            for (int i = 0; i < 10; i++ )
            {
                using (var adp = new OneDriveBlobAdapter("/OneDrive/", ClientId, ClientSecret, AccessToken, RefreshToken, "/Public/", null))
                {
                    var sw = Stopwatch.StartNew();
                    var list = adp.GetFilesAsync("/OneDrive/", 0, false).Result;
                    Debug.WriteLine(sw.Elapsed.ToString());

                    var itemBDire = new BlobDirectory(null, "/OneDrive/Sub1/Sub2/");
                    adp.CreateDirectoryAsync(itemBDire).Wait();
                    Debug.WriteLine(sw.Elapsed.ToString());
                    var list2 = adp.GetFilesAsync("/OneDrive/Sub1/", 0, true).Result;
                    Debug.WriteLine(sw.Elapsed.ToString());
                    Assert.IsTrue(list2.Count > 0);
                }
            }
        }

    }
}
